package hu.myai.indicators;

import hu.myai.api.indicators.AbstractIndicator;
import hu.myai.model.IndicatorVO;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author bozbalint
 *
 */
public class MovingAverageIndicator extends AbstractIndicator {

    /**
     * The object used when logging debug,errors,warnings and info.
     * */
    private final Logger log = LoggerFactory.getLogger(this.getClass());

    public MovingAverageIndicator(Double parameter) {
        super(parameter);
    }

    @Override
    public void init() {
    }

    @Override
    protected IndicatorVO indicatorCalculation() {

        double average = 0;
        if (tickHistory.size() < parameter)
            return null;

        for (int i=0; i < parameter.intValue(); i++) {
            average += tickHistory.get(i).getClose();
        }

        average /= parameter;

        IndicatorVO indicator = new IndicatorVO();
        indicator.setValue(average);
        indicator.setChanged(true);
        indicator.setDate(lastTick.getTime());

        log.debug(indicator.toString());
        return indicator;
    }
}
